<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />

		<style>
			.ui-page-login,
			body {
				width: 100%;
				height: 100%;
				margin: 0px;
				padding: 0px;
			}
			
			.mui-content {
				height: 100%;
				background-color: #3A46A1;
			}
			
			#logo {
				width: 128px;
				height: 52.5px;
				position: absolute;
				top: 50%;
				left: 50%;
				margin-top: -26px;
				margin-left: -64px;
			}
			
			#login {
				background: #3A46A1;
				border-radius: 25px;
				color: #9BA1CC;
				width: 80%;
				margin: 0 auto;
				margin-top: 20px;
				border: solid #5F69B0 2px;
			}
			
			.mui-btn {
				padding: 10px;
			}
		</style>

	</head>

	<body>

		<div class="mui-content">

			<div id="logoLayout" style="height: 40%; width: 100%; position: relative;">

				<img id="logo" src="images/logo.png" />

			</div>

			<div id="loginLayout">

				<div style="width: 78%; margin: 0 auto; vertical-align: bottom; ">
					<img src="images/username.png" style="width: 24px; height: 24px; float: left;" />
					<p style="font-size: 20px; color: #9BA1CC; margin-left: 35px;">Username</p>
				</div>

				<form id="login-form" style="width: 80%; margin: 0 auto; margin-top: 10px;">
					<div class="mui-input-row">
						<input id='account' type="text" class="mui-input-clear mui-input" style="border-radius: 25px;">
					</div>
				</form>

				<div style="width: 78%; margin: 0 auto; vertical-align: bottom; margin-top: 5px;">
					<img src="images/password.png" style="width: 24px; height: 24px; float: left;" />
					<p style="font-size: 20px; color: #9BA1CC; margin-left: 35px;">Password</p>
				</div>

				<form id="login-form" style="width: 80%; margin: 0 auto; margin-top: 10px;">
					<div class="mui-input-row">

						<input id='password' type="password" class="mui-input-clear mui-input" style="border-radius: 25px;">
					</div>
				</form>

			</div>

			<button id='login' class="mui-btn mui-btn-block mui-btn-primary">Login</button>

		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script src="js/app.js"></script>
		<script src="js/md5.min.js"></script>
		<script type="text/javascript" src="js/http.js"></script>
		<script>
			(function($, doc) {
				$.init({
					statusBarBackground: '#3A46A1'
				});

				$.plusReady(function() {

					function toMain() {
						var mainPage = $.openWindow({
							"id": 'search',
							"url": 'search.html'
						});

						var id = setInterval(function() {
							clearInterval(id);
							$.fire(mainPage, 'show', null);
							mainPage.show("pop-in");
							var ws = plus.webview.currentWebview();
							plus.webview.close(ws);
						}, 20);
					};

					// close splash
					setTimeout(function() {
						//关闭 splash
						plus.navigator.closeSplashscreen();
					}, 600);

					var loginButton = doc.getElementById('login');
					var accountBox = doc.getElementById('account');
					var passwordBox = doc.getElementById('password');

					var user = JSON.parse(localStorage.getItem('$user') || '{}');
					console.log("登录user数据---> " + JSON.stringify(user));

					if(user.account != null) {
						accountBox.value = user.account;
					}
					if(user.password != null) {
						passwordBox.value = user.password;
					}

					loginButton.addEventListener('tap', function(event) {

						if(accountBox.value.trim().length <= 0) {
							plus.nativeUI.toast('Username is required!');
							return;
						}
						if(passwordBox.value.trim().length <= 0) {
							plus.nativeUI.toast('Password is required!');
							return;
						}

						var postdata = {
							username: accountBox.value.trim().toLocaleLowerCase(),
							password: md5(passwordBox.value.trim().toLocaleLowerCase()),
							version: '0.1',
							platform: 'merchant_android',
							token: 'f416b7333f412805f5f1def594bdcf93'
						};

						http.asyncHttp('loginMerchant.jsp', postdata, function(result) {
								if(result.match("success") != null) {
									var loginInfo = {
										account: accountBox.value.trim(),
										password: passwordBox.value.trim()
									};
									localStorage.setItem('$user', JSON.stringify(loginInfo));
									toMain();
								} else {
									plus.nativeUI.toast(JSON.parse(result).Result);
									return;
								}
							},
							function(err) {
								plus.nativeUI.toast("请求出错了");

							});
					});
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});

					//
					var backButtonPress = 0;
					$.back = function(event) {
						backButtonPress++;
						if(backButtonPress > 1) {
							plus.runtime.quit();
						} else {
							plus.nativeUI.toast('再按一次退出应用');
						}
						setTimeout(function() {
							backButtonPress = 0;
						}, 1000);
						return false;
					};

				});
			}(mui, document));
		</script>
	</body>

</html>